package com.goods.service;

import com.goods.entity.Goods;
import com.goods.entity.GoodsCategory;
import com.goods.model.Page;
import com.goods.util.DBManager;
import com.goods.util.GoodsNoUtil;
import com.goods.util.IDGen;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.*;

/**
 * 商品类别业务逻辑类
 * @author huangyanzhong
 * @date 2020-04-01
 */
@Service
public class GoodsCategoryServiceImpl implements GoodsCategoryService{

    @Autowired
    private DBManager dbManager;


    /**
     * 获取商品类别详情
     * @param id
     * @return
     */
    @Override
    public GoodsCategory get(String id) throws Exception{
        String sql="select * from goods_category where id=?";
        List<Map<String,Object>> list=dbManager.select(sql,id);
        if(list==null||list.isEmpty()){
            return null;
        }
        Map<String,Object> map=list.get(0);
        GoodsCategory goodsCategory=parse(map);
        return goodsCategory;
    }

    /**
     * 转换
     * @param map
     * @return
     */
    private GoodsCategory parse(Map<String,Object> map){
        GoodsCategory goodsCategory=new GoodsCategory();
        goodsCategory.setId((String)map.get("id"));
        goodsCategory.setName((String)map.get("name"));
        return goodsCategory;
    }

    /**
     * 获取所有商品列表
     * @return
     * @throws Exception
     */
    @Override
    public List<GoodsCategory> getAll() throws Exception{
        String sql="select * from goods_category where 1=1 ";
        List<Map<String,Object>> list=dbManager.select(sql,null);
        if(list==null||list.isEmpty()){
            return new ArrayList<>();
        }
        List<GoodsCategory> goodsCategoryList=new ArrayList<>();
        for(Map<String,Object> map:list){
            goodsCategoryList.add(parse(map));
        }
        return goodsCategoryList;
    }

}
